library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout

Read in data

path <- "../data/marathon_results_2017.csv"
marathon <- read_csv(path)
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 26410 Columns: 22
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (10): Bib, Name, M/F, City, State, Country, 10K, 15K, 20K, Proj Time
## dbl   (4): Age, Overall, Gender, Division
## time  (8): 5K, Half, 25K, 30K, 35K, 40K, Pace, Official Time
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
colnames(marathon)
##  [1] "Bib"           "Name"          "Age"           "M/F"          
##  [5] "City"          "State"         "Country"       "5K"           
##  [9] "10K"           "15K"           "20K"           "Half"         
## [13] "25K"           "30K"           "35K"           "40K"          
## [17] "Pace"          "Proj Time"     "Official Time" "Overall"      
## [21] "Gender"        "Division"

Interactive Plot

Top 25 runners

p1_data <- marathon %>% 
  group_by(Country) %>% 
  summarise(fastest_time = min(`Official Time`),
            fastest_runner = Name[which.min(`Official Time`)]) %>% 
  arrange(fastest_time) %>% 
  head(15)
  • plot the data
top15 <- ggplot(data = p1_data, mapping = aes(x = fastest_time, y = fct_reorder(Country, fastest_time, .desc = TRUE))) +
  geom_col(aes(fill = Country), color = "lightgray", alpha = 0.3) +
  geom_text(aes(label = `fastest_runner`),  nudge_x = -3000, ) +
  scale_x_continuous(limits = c(0, 11000)) +
  labs(
    title = "Top 15 Runners by Country",
    subtitle = "2017 Boston Marathon",
    x = "Time in Seconds",
    y = ""
  ) +
  theme(
    legend.position = "none",
    plot.title = element_text(hjust=0.5, vjust=2, size=14),
    plot.subtitle = element_text(hjust=0.5, vjust=3)
  )

top15

ggplotly(top15)

Spacial visualization of the number of racers per state in USA

unwanted <- c("AA", "AE","AP","MH","GU","PR","VI","DC")
USA_runners <- 
  marathon %>% 
  filter(Country == "USA") %>% 
  filter(!State %in% unwanted) %>% 
  group_by(State) %>% 
  summarise(num_runners = n())%>% 
  rename(state = State)
USA_runners %>% 
  filter(state == "MA") %>% 
  pull
## [1] 4586
USA_runners %>% 
  filter(!state == "MA") %>% 
  summarise(total_other = sum(num_runners))
## # A tibble: 1 × 1
##   total_other
##         <int>
## 1       16171

Using usmap to create spatial visualization

  • I attempted to use the USA shapefiles and sf package
    • However, I learned a very hard lesson in trying to manually adjust size and shape of the entir plot margins
    • I could not get it zoomed in enough to see details
  • Which lead me to use this package
library(usmap)
plot_usmap(data = USA_runners, values = "num_runners") +
  scale_fill_continuous(low = "lightblue", high = "black", name = "Number of Runners", label = scales::comma) + 
  labs(title = "US Participation by State", subtitle = "2017 Boston Marathon") +
  theme(
    plot.title = element_text(hjust=0.5, vjust=0, size=14),
    plot.subtitle = element_text(hjust=0.5, vjust=0),
    legend.position = "right"
    )

us_participation <- plot_usmap(data = USA_runners, values = "num_runners") +
  scale_fill_continuous(low = "lightblue", high = "black", name = "Number of Runners", label = scales::comma) + 
  labs(title = "US Participation by State", subtitle = "2017 Boston Marathon") +
  theme(
    plot.title = element_text(hjust=0.5, vjust=0, size=14),
    plot.subtitle = element_text(hjust=0.5, vjust=0)
    )

ggplotly(us_participation)

Models

How much does age affect your official time

  • Hypothesize the relationship between Age and Official Time to be highly positively correlated
  • Why?
    • As we get older, our marathon times would increase
    • However we can see that in the ages from 20-40 there are many runners who had extrememly fast times
  • Conclusion is in general, yes there is a positive correlation, it is not highly correlated however as we can tell from our linear model
ggplot(data = marathon, aes(x = Age, y = `Official Time`)) + 
  geom_point( alpha = 0.1) +
  geom_smooth(method = "lm") +
  facet_wrap(~`M/F`)
## `geom_smooth()` using formula = 'y ~ x'

Let’s take a look at a highly correlated relationship

  • Hypothesize the faster a runner reaches halfway, it does not necessarily mean they will complete the marathon faster than others
  • Why?
    • It could be that runners are not focusing enough and run faster during the first half and get too tired to keep up the pace
  • Conclusion, for the 2017 Boston Marathon, the relationship between the time it took a runner to get to halfway and finish is highly correlated
ggplot(data = marathon, aes(x = Half, y = `Official Time`)) + 
  geom_point( alpha = 0.1) +
  geom_smooth(aes(color = `M/F`), method = "lm") +
  facet_wrap(~`M/F`) +
  theme(
    legend.position = "none"
  )
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values (`geom_point()`).

Let’s take a look at an extreme correlation

  • Hypothesize the quicker a runner’s pace is, the faster they will finish
  • Why?
    • The math just seems to add up here :)
  • Conclusion, extreme correlation!
ggplot(data = marathon, aes(x = Pace, y = `Official Time`)) + 
  geom_point( alpha = 0.1) +
  geom_smooth(aes(color = `M/F`), method = "lm") +
  facet_wrap(~`M/F`) +
  theme(
    legend.position = "none"
  )
## `geom_smooth()` using formula = 'y ~ x'